/*
 * TablePanel.cpp
 *
 * Copyright 2012 Pieter Agten
 *
 * This file is part of tltgen.
 *
 * tltgen is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * tltgen is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with tltgen.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @file   TablePanel.cpp
 * @author Pieter Agten (pieter.agten@gmail.com)
 * @date   3 sep 2012
 */


#include <wx/notebook.h>
#include <wx/aui/auibook.h>
#include <boost/shared_ptr.hpp>

#include "GridPanel.hpp"
#include "ChartPanel.hpp"
#include "CelsiusScale.hpp"
#include "Plot.hpp"

#include "TablePanel.hpp"

using boost::shared_ptr;

namespace tltgen {

  BEGIN_EVENT_TABLE(TablePanel, wxPanel)
  END_EVENT_TABLE()

  TablePanel::TablePanel(wxWindow* parent, wxWindowID id, const shared_ptr<ThermistorTable>& thermistorTable, const shared_ptr<LeastSquaresSolver>& solver, const shared_ptr<TemperatureScale>& temperatureScale, const wxPoint& pos, const wxSize& size) : 
  wxPanel(parent, id, pos, size),
    thermistorTable(thermistorTable)
  {
    wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
    SetSizer(sizer);

    this->notebook = new wxAuiNotebook(this, ID_ViewNotebook, wxDefaultPosition, wxDefaultSize, wxAUI_NB_BOTTOM);

    this->gridPanel = new GridPanel(notebook, ID_GridPanel, thermistorTable, solver, temperatureScale, wxDefaultPosition, wxDefaultSize);
    this->notebook->AddPage(gridPanel, _("Grid"), true);
   
    sizer->Add(notebook, 1, wxEXPAND, 0);
  }

  TablePanel::~TablePanel()
  { }

  bool TablePanel::isValidThermistorTable(const shared_ptr<ThermistorTable>& thermistorTable)
  {
    return GridPanel::isValidThermistorTable(thermistorTable);
  }

  void TablePanel::addPlot(const shared_ptr<Plot>& plot)
  {
    ChartPanel* chartPanel = new ChartPanel(this->notebook, wxID_ANY, plot, wxDefaultPosition, wxDefaultSize);
    this->notebook->AddPage(chartPanel, wxString::FromUTF8(plot->getTitle().c_str()), false);
  }

  void TablePanel::addThermistorModel(const shared_ptr<ThermistorModel>& thermistorModel)
  {
    this->gridPanel->addThermistorModel(thermistorModel);
  }
 
  bool TablePanel::isValidTemperatureScale(const shared_ptr<TemperatureScale>& temperatureScale) const
  {
    return this->gridPanel->isValidTemperatureScale(temperatureScale);
  }

  void TablePanel::setTemperatureScale(const shared_ptr<TemperatureScale>& temperatureScale)
  {
    if (! isValidTemperatureScale(temperatureScale)) {
      throw std::invalid_argument("Given temperature scale is not valid.");
    }

    this->gridPanel->setTemperatureScale(temperatureScale);
  }

  shared_ptr<ThermistorTable> TablePanel::getThermistorTable() const
  {
    return this->thermistorTable;
  }

}


